﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HelloLLLLL.Blog.Models;
using HelloLLLLL.Blog.DAL;
using System.Text;

namespace HelloLLLLL.Blog.html
{
    public partial class Comment : System.Web.UI.Page
    {
        public List<LeaveMessage> ListParent = null;
        DBHelper helper = new DBHelper();
        public StringBuilder Sb = new StringBuilder();
        protected void Page_Load(object sender, EventArgs e)
        {
            GetPL();
        }

        private void GetPL()
        {
            List<LeaveMessage> listAllPL = new ModelHandler<LeaveMessage>().FillModel(helper.GetDataTable("select * from LeaveMessage order by id desc", null));
            if (listAllPL != null)
            {
                ListParent = listAllPL.Where(p => p.ReplyId == 0).ToList();


                LoopGetPL(ListParent, listAllPL);

            }


        }
        private void LoopGetPL(List<LeaveMessage> list, List<LeaveMessage> listAllPL)
        {
            foreach (var item in list)
            {
                item.ChildPL = listAllPL.Where(p => p.ReplyId == item.Id).ToList();

                LoopGetPL(item.ChildPL, listAllPL);
            }
        }

        public string LoopList(List<LeaveMessage> list, string parent, int parentId)
        {
            if (list != null)
            {
                string str = @"<div class='comment-child'><a name ='reply-@reply.Id' ></a><img src ='../res/images/Leo.jpg' alt = {0} /><div class='info'><span class='username'>{1}</span><span style = 'padding-right:0;margin-left:-5px;'> 回复 </span ><span class='username'>{2}</span><span>{3}</span></div><p class='info'><span class='time'>{4}</span><a href = 'javascript:;' class='btn-reply' data-targetname='{5}'  data-targetid='{6}'>回复</a></p></div>";



                for (int i = 0; i < list.Count; i++)
                {
                    string ss = string.Format(str, list[i].Pubnisher, list[i].Pubnisher, parent, list[i].Content, list[i].CTime, list[i].Pubnisher, list[i].Id);

                    Sb.Append(ss);

                    LoopList(list[i].ChildPL, list[i].Pubnisher, list[i].Id);
                }
            }
            if (Sb.Length > 0)
            {
                return Sb.ToString();
            }
            return "";
        }
    }
}